home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / p2c / doc / p2c.man.wn (TXT.LB) < prev   
Encoding:
Text File  |  1994-11-22  |  49.4 KB  |  856 lines  |  [TEXT/nX^n]

  1.  
  2. NAME     
  3.         p2c - Pascal to C translator, version 1.21
  4.  
  5. SYNOPSIS     
  6.         p2c [ options ] [ file [ module ] ] 
  7.  
  8. DESCRIPTION 
  9. P2c is a tool for translating Pascal programs into C. The input consists of a set of source files in 
  10. any of the following Pascal dialects: HP Pascal, Turbo/UCSD Pascal, DEC VAX Pascal, 
  11. Oregon Software Pascal/2, Macintosh Programmer's Workshop Pascal, Sun/Berkeley Pascal, 
  12. Texas Instruments Pascal, Apollo Domain Pascal.. Modula-2 syntax is also supported. Output is 
  13. a set of .c and .h files that comprise an equivalent program in any of several dialects of C. 
  14. Output code may be kept machine- and dialect-independent, or it may be targeted to a specific 
  15. machine and compiler. Most reasonable Pascal programs are converted into fully functional C 
  16. which will compile and run with no further modifications, although p2c sometimes chooses to 
  17. generate readable code at the expense of absolute generality. 
  18.  
  19. P2c endeavors to insert notes and warning messages into the output code to point out areas 
  20. which may require human intervention. Output code is arranged to be readable and efficient, and 
  21. to make use of C idioms wherever possible. The main goal of the translation is to produce C 
  22. files which are pleasant and "natural" enough to be acceptable as the new source files for a pro
  23. gram. In a pinch, p2c will also serve as an ad hoc Pascal compiler.  The p2cc script makes it 
  24. easy to use p2c as a compiler.
  25.  
  26. Code generated by p2c normally does not assume characters are signed or unsigned. Also, it 
  27. assumes int is the same as either short or long but does not depend on which. However, if int is 
  28. not the same as long it is best to use a modern C compiler which supports prototypes. Generated 
  29. code does not require an ANSI-compatible compiler (unless ANSI-style code is requested), but 
  30. it does use various ANSI-standard library routines.
  31.  
  32. All generated code includes the file <p2c.h> which in turn includes <stdio.h> and various other 
  33. common resources. Also, many translated programs will need to be linked with the run-time 
  34. library, typically -lp2c.
  35.  
  36. Given a file name, p2c reads from the specified file and outputs to a file with a .c suffix added or 
  37. substituted. For example, 
  38.     p2c myfile.p
  39.  
  40. reads from myfile.p to produce the file myfile.c. The input file may contain a Pascal main 
  41. program or a single Pascal module (or "unit" in UCSD Pascal nomenclature), or it may just 
  42. contain a number of procedures and declarations. P2c is designed to work for correct input 
  43. programs. That is, it will accept partial programs but may occasionally crash if the input refers to 
  44. undefined symbols.
  45.  
  46. If the input is a module, the translator will also produce a file module .h containing a translation 
  47. of the module's interface section. The implementation section may be omitted in which case only 
  48. the .h file will be interesting. If the program or module has include files, these may cause 
  49. additional .c files to be generated depending on the value of the ExpandIncludes option (see 
  50. below).
  51.  
  52. If no file name is given, p2c reads Pascal from the standard input and writes the resulting C to 
  53. standard output (though a .h file may still be produced). If a file name and module name are 
  54. given, the file may include several modules (or units). The specified module is translated; any 
  55. others are skipped. The output files will be named module .c and module .h . P2c never 
  56. translates more than one module per run.
  57.  
  58. The input program or module may make use other modules. These modules may be referred to 
  59. explicitely.  P2c holds a search list of places to find modules which are imported, the ImportDir 
  60. search list. Modules for inlusion may as well reside in additional default input files which are 
  61. imported before beginning the translation.
  62.  
  63. When starting, p2c a number of configuration parameters are set. The default parameters are 
  64. stored in p2c’s resource fork as resource of type 'p2c  '. These default parameters can be modi
  65. fied using ResEdit. Next the file "p2c preferences" is read for a number of configuration para
  66. meters from the preferences folder. (The actual path used on your system may vary. The -i 
  67. option is a handy way to examine this file.) If the P2CRC environment variable is set, it gives 
  68. the name of a file to read instead of the system file; this file can start with Include 
  69. %H:"p2c preferences" to include the system file. Next, p2c attempts to read the file 
  70. “p2c preferences” in your input directory for further configuration.
  71.  
  72.  
  73. OPTIONS
  74. Note: this description refers to the p2c version running as an MPW tool. P2c is also available as 
  75. a stand-alone application. In the stand-alone application, use the menus to set the options. 
  76.  
  77. -o cfile    Use cfile in place of file .c or module .c as the primary output file. A single dash 
  78. ('–o –') says to write the C code to the standard output.
  79.  
  80. -h hfile    Use hfile in place of module .h as the output file for interface text. This only has 
  81. effect if the input is an HP Pascal module or a Pascal unit.
  82.  
  83. -s sfile     Read interface text from sfile before beginning the translation. This file typically 
  84. contains one or more modules, often with implementation sections omitted for 
  85. speed, which the program or module being translated will use.  (Typically the 
  86. ImportFrom and ImportDir parameters in p2crc are set up to allow p2c to locate 
  87. interface text without needing any -s options.) If there are several -s options in the 
  88. command, the sfiles are read from left to right.
  89.  
  90. -p n    Display progress of translation in the form of a line number/file name display. This 
  91. is refreshed every n lines, 25 by default.
  92.  
  93. -c prefsfile    Read local configuration commands from prefsfile instead of "p2c preferences".  A 
  94. dash ('–c –') in place of prefsfile causes no local configuration file to be used.
  95.  
  96.  -v    ("Vanilla.") Do not read from the system configuration file "p2c preferences". Since 
  97. some of the parameters in this file are required, the p2c resource must include those 
  98. parameters instead. This also suppresses the file named by the P2CRC environment 
  99. variable.
  100.  
  101. -H homedir    Use homedir as the p2c home directory. The system "p2c preferences" file will be 
  102. searched for in this directory. 
  103.  
  104. -I pattern    Add pattern to the ImportDir search list of places to find modules which are 
  105. imported. The pattern should include a %s to represent the module name, and 
  106. should evaluate to a potential file name for that module's source code. For example, 
  107. ::%s.p looks for <modulename>.p in the parent of the current directory.
  108.  
  109.  -i    This special option copies the p2c system configuration to the standard output in its 
  110. entirety. (It may be used with -H , but -i is most useful precisely when you don't 
  111. know the location of the home directory.)
  112.  
  113.  -q    Quiet mode. Suppresses output of status messages during translation.
  114.  
  115. -E n    Abort translation after n errors. If n is omitted it defaults to zero, which means 
  116. unlimited errors are allowed. Use -E 1 to make p2c halt after the first error.
  117.  
  118.  -e    Echo the Pascal source into the output file, surrounded by #ifdefs. This is the same 
  119. as the CopySource parameter in the "p2c preferences" file.
  120.  
  121.  -a    Produce modern ANSI C. This is a convenient override for the AnsiC parameter in 
  122. the "p2c preferences" file.
  123.  
  124. -L language    Select input language name, such as MPW, VAX or TURBO. This is a convenient 
  125. override for the Language parameter.
  126.  
  127.  -V    Verbose mode. This causes p2c to generate an additional ".log" file with further 
  128. details of the translation, such as a list of warnings and notes including those which 
  129. are suppressed in the regular output.
  130.  
  131.  -comp    Compiler mode.  This switch tells p2c to use various configuration defaults that are 
  132. more suitable for use as a Pascal compiler rather than a translator.  It is the same as 
  133. specifying the following options in your "p2c preferences" file:
  134.         ElimDeadCode 0
  135.         AnalyzeFlow 0
  136.         MaxLineBreakTies 0
  137.         FoldConstants 1
  138.         FoldStrConstants 1
  139.         OffsetForLoops 0
  140.         StaticLinks 1
  141.         BitwiseMod 0
  142.         BitwiseDiv 0
  143.         AssumeBits 0
  144.         AssumeSigns 0
  145.         FormatStrings 1
  146.         StructFiles 1
  147.         FullStrWrite 1
  148. The p2cc script specifies this option when it runs p2c to compile a Pascal program.
  149.  
  150.  -local    Local settings.  This switch uses various configuration defaults that are appropriate 
  151. if the code generated by p2c is going to be compiled and run on the same machine 
  152. that ran p2c itself.
  153.  
  154.  -check    Enable all error checking.  Normally, some error checks are off by default, as 
  155. described in the comments in the system "p2c preferences" file.
  156.  
  157.  -M0    Disable memory conservation. This prevents p2c from freeing various data 
  158. structures after translating each function, in case this new conservation feature 
  159. causes unforseen problems.
  160.  
  161.  -R    Regression testing mode. Formats notes and warning messages in a way that makes 
  162. it easier to run compare on the output of p2c.
  163.  
  164. P2c also understands a few debugging options which may occasionally be useful when tracking 
  165. down translation problems. The -d n option sets the "debug level" to n, a small integer which is 
  166. normally zero. Debugging output is written into the regular output file along with the C code; the 
  167. higher your n, the more "wallpaper" you get. Also, -t prints debugging information at every 
  168. Pascal token, -B n enables line-breaker debugging, -C n enables comment placement debugging, 
  169. and -F n enables flow-analysis debugging.
  170.  
  171.  
  172. CHOICE OF SOURCE LANGUAGE 
  173. The Language configuration parameter or -L command-line option tells p2c which Pascal dialect 
  174. to expect in the input file. Any language features which do not overlap between dialects are 
  175. supported all of the time. The Language parameter is consulted when a syntax or usage is 
  176. detected that has different meanings in two different dialects, and also to determine default 
  177. values for various other translation parameters as described below.
  178.  
  179. The following language words are supported by p2c. Names are case-insensitive.
  180.  HP    HP Pascal. All features of HP Standard Pascal, the Pascal Workstation version, are 
  181. supported except as noted in BUGS below. Some features of MODCAL, HP's 
  182. extended Pascal, are also supported. This is a superset of ISO standard Pascal, 
  183. including conformant arrays and procedural parameters.
  184.  
  185.  HP-UX     HP Pascal, HP-UX version. Almost identical to the "HP" dialect.
  186.  
  187.  Turbo     Turbo Pascal 5.0 for the IBM PC. Few conflicts with HP Pascal, so the Language 
  188. parameter is not often needed for Turbo. (Most important is that the Turbo and HP 
  189. dialects use 16 and 32 bit integers, respectively.)
  190.  
  191.  UCSD     UCSD Pascal. Similar to Turbo in many ways.
  192.  
  193.  MPW     Macintosh Programmer's Workshop Pascal 2.0. This is the default language. 
  194. Should also do a pretty good job for Think Pascal. Object Pascal features are not 
  195. supported, nor is the fact that char variables are sometimes stored in 16 bits.
  196.  
  197. VAX     VAX/VMS Pascal version 3.5. Most but not all language features supported. This 
  198. has not yet been tested on large programs.
  199.  
  200. Oregon     Oregon Software Pascal/2. All features implemented.
  201.  
  202. Berk     Berkeley Pascal with Sun extensions.
  203.  
  204. TIP    Texas Instruments Pascal.
  205.  
  206. Apollo    Apollo Domain Pascal.
  207.  
  208. Modula     Modula-2. Based on Wirth's Programming in Modula-2, 3rd edition. Proper setting 
  209. of the Language parameter is not optional. Translation will be incomplete in most 
  210. cases, but should be good enough to work with. Structure of local sub-modules is 
  211. essentially ignored; like-named identifiers may be confused. Type WORD is 
  212. translated as an integer, but type ADDRESS is translated as char * or void *; this 
  213. may cause inconsistencies in the output code.
  214.  
  215.     Modula-2 modules have two parts in separate files. Suppose these are called 
  216. foo.def (definition part) and foo.mod (implementation part) for module foo. Then a 
  217. pattern like %s.def must be included in the ImportDir list, and LibraryFile must be 
  218. changed to refer to system.m2 instead of system.imp. To translate the definition 
  219. part, give the command
  220.          p2c foo.def
  221.     to translate the definition part into files foo.h and foo.c ; the latter will usually be 
  222. empty. The command
  223.         p2c -s foo.def foo.mod
  224.     will translate the implementation part into file foo.c.
  225.  
  226. Even if all language features are supported for a dialect, some predefined functions may be 
  227. omitted. In these cases, the function call will be translated literally into C with a warning. Some 
  228. hand modification may be required. 
  229.  
  230. CONFIGURATION PARAMETERS 
  231. P2c is highly configurable. The defaults are suitable for most applications, but customizing these 
  232. parameters will help you get the best possible translation. Since the output of p2c is intended to 
  233. be used as human-maintainable source code, there are many parameters for describing the 
  234. coding style and conventions you prefer. Others give hints about your program that help p2c to 
  235. generate more correct, efficient, or readable code.
  236.  
  237. The p2crc files contain a list of parameters, one per line. The system configuration file, which 
  238. may be viewed using the -i option to p2c, serves as an example of the proper format. Parameter 
  239. names are case-insensitive. If a parameter name occurs exactly once in the system p2crc, this 
  240. indicates that it must have a unique value and the last value given to it by the configuration files 
  241. is used. Other parameters are written several times in a row; these are lists to which each 
  242. configuration line adds an entry.
  243.  
  244. Many p2crc options take a numeric value of 0 or 1, roughly corresponding to "no" or "yes." 
  245. Sometimes a blank value or the value "def" corresponds to an intermediate "maybe" state. For 
  246. example, the stylistic option ExtraParens switches between copious or minimal parentheses in 
  247. expressions, with the default being a nice compromise intended to be best for readers with an 
  248. average knowledge of C operator precedences.
  249.  
  250. Configuration options may also be embedded in the source file in the form of Pascal comments:
  251.  
  252.     {ShortOpt=0} {AvoidName=fred}
  253.     {FuncMacro slope(x,y)=atan2(y,x)*RadDeg}
  254.  
  255. disables automatic short-circuiting of and and or expressions, adds "fred" to the list of names to 
  256. avoid using in generated C code, and defines a special translation for the Pascal program's slope 
  257. function using the standard C atan2 function and a constant RadDeg presumably defined in the 
  258. program. Whitespace is generally not allowed in embedded parameters. The `=' sign is required 
  259. for embedded parameters, though it is optional in p2crc files. Comments within embedded 
  260. parameters are delimited by `##'. Numeric parameters may replace `=' with `+' or `-' to increase 
  261. or decrease the parameter; list-based parameters may use `-' to remove a name from a list rather 
  262. than adding it. Also, the parameter name by itself in comment braces means to restore the 
  263. parameter's value that was current before the last change:
  264.  
  265.     {VarFiles=0 ## Pass FILE *'s params by value even if VAR}    some declarations
  266.     {VarFiles  ## Back to original FILE * passing}
  267.  
  268. causes the parameter VarFiles to have the value 0 for those few declarations, without affecting 
  269. the parameter's value elsewhere in the file.
  270.  
  271. If an embedded parameter appears in an include file or in interface text for a module, the effect of 
  272. the assignment normally carries over to any programs that included that file. If the parameter 
  273. name is preceded by a `*', then the assignment is automatically undone after the source file that 
  274. contains it ends:
  275.  
  276.     {IncludeFrom strings=<p2c/strings.h>}
  277.     {*ExportSymbol=pascal_%s}
  278.     module strings;
  279.  
  280. will record the location of the strings module's include file for the rest of the translation, but the 
  281. assignment of ExportSymbol pertains only to the module itself.
  282.  
  283. For the complete list of p2crc parameters, run p2c with the -i option. Here are some additional 
  284. comments on selected parameters:
  285.  
  286. ImportAll
  287. Because Turbo Pascal only allows one unit per source file, p2c normally stops reading past 
  288. the word implementation in a file being scanned for interface text. But HP Pascal allows 
  289. several modules per file and so this would not be safe to do. The ImportAll option lets you 
  290. override the default behavior for your Pascal dialect.
  291.  
  292. AnsiC
  293. This parameter selects which dialect of C to use. If 1, all conventions of ANSI C such as 
  294. prototypes, void * pointers, etc. are used. If 0, only strict K&R (first edition) C is used. The 
  295. default is to use "traditional UNIX C," which includes enum and void but not void * or 
  296. prototypes. Once again there are a number of other parameters which may be used to control 
  297. the individual features if just setting AnsiC is not enough.
  298.  
  299. C++
  300. This tells p2c to use a number of language extensions present in C++:  Specifically, it 
  301. enables the "//" format for comments, use of "anonymous unions" for variant records, use of 
  302. declarations within the function body, use of references for VAR parameters, and use of 
  303. "new" and "delete" instead of "malloc" and "free". P2c will check for collisions with C++ 
  304. reserved words unless you explicitly set the C++ option to zero.
  305.  
  306. TurboObjects
  307. P2c recognizes two major dialects of object-oriented Pascal.  Turbo Pascal 6.0 object
  308. types translate fairly directly into C++ classes.  In Apple's Object Pascal, the object
  309. type has similar syntax but represents a handle (a double pointer) to an object rather than an 
  310. object itself.  TheTurboObjects option (whose default is determined by the Language 
  311. setting) says whether objects should be direct or indirect through pointers. ( P2c uses 
  312. pointers instead of handles; p2c is most often used to make programs more portable, and 
  313. few systems except the Mac use handles in this way.)
  314.  
  315. UseVExtern
  316. Many non-UNIX linkers prohibit variables from being defined (not declared) by more than 
  317. one source file. One module must declare, e.g., "int foo;", and all others must declare 
  318. "extern int foo;". P2c accomplishes this by declaring public variables "vextern" in header 
  319. files, and arranging for the macro vextern to expand to extern or to nothing when 
  320. appropriate. If you set UseVExtern =0 p2c will instead declare variables in a simpler way 
  321. that works only on UNIX-style linkers.
  322.  
  323. UseAnyptrMacros
  324. Certain C reserved words have meanings which may vary from one C implementation to 
  325. another. P2c uses special capitalized names for these words; these names are defined as 
  326. macros in the file p2c.h which all translated programs include. You can set 
  327. UseAnyptrMacros = 0 to disable the use of these macros. Note that the functions of many 
  328. of these macros can also be had directly using other parameters; for example, UseConsts 
  329. allows you to specify whether your target language recognizes the word const in constant 
  330. declarations. The default is to use the Const macro instead, so that your code will be portable 
  331. to either kind of implementation.
  332.  
  333. Signed expands to the reserved word signed if that word is available, otherwise it is given a 
  334. null definition. Similarly, Const expands to const if that feature is available. The words 
  335. Volatile and Register are also defined in p2c.h, although p2c does not use them at present. 
  336. The word Char expands to char by default, but might need to be redefined to signed char or 
  337. unsigned char in a particular implementation. This is used for the Pascal character type; 
  338. lowercase char is used when the desired meaning is "byte," not "character."
  339.  
  340. The word Static always expands to static by default. This is used in situations where a 
  341. function or variable is declared static to make it local to the source file; lowercase static is 
  342. used for static local variables. Thus you can redefine Static to be null if you want to force 
  343. private names to be public for purposes of debugging.
  344.  
  345. The word Void expands to void in all cases; it is used when declaring a function with no 
  346. return value. The word Anyptr is a typedef for void * or char * as necessary; it represents a 
  347. generic pointer.
  348.  
  349. UsePPMacros
  350. The p2c.h header also declares two macros for function prototyping, PP (x) and PV (). 
  351. These macros are used as follows:
  352.  
  353.         Void foo PP( (int x, int y, Char *z) );
  354.         Char *bar PV( );
  355.  
  356. If prototypes are available, these macros will expand to
  357.  
  358.         Void foo (int x, int y, Char *z);
  359.         Char *bar (void);
  360.  
  361. but if only old-style declarations are supported, you instead get
  362.  
  363.         Void foo ();
  364.         Char *bar ();
  365.  
  366. By default, p2c uses these macros for all function declarations, but function definitions are 
  367. written in old-style C. The UsePPMacros parameter can be set to 0 to disable all use of PP 
  368. and PV , or it can be set to 1 to use the macros even when defining a function. (This is 
  369. accomplished by preceding each old-style definition with a PP -style declaration.) If you 
  370. know your code will always be compiled on systems that support prototyping, it is prettier 
  371. to set Prototypes =1 or simply AnsiC =1 to get true function prototypes.
  372.  
  373. EatNotes
  374. Notes and warning messages containing any of these strings as sub-strings are not emitted.  
  375. Each type of message includes an identifier like [145] ; you can add this identifier to the 
  376. EatNotes list to suppress that message. Another useful form is to use a variable name or 
  377. other identifier to suppress warnings about that variable. The strings are a space-separated 
  378. list, and thus may not contain embedded spaces. To suppress notes around a section of 
  379. code, use, e.g., {EatNotes+[145]} and {EatNotes-[145]}. Most notes are generated during 
  380. parsing, but to suppress those generated during output the string may need to remain in the 
  381. list far beyond the point where it appears to be generated. Use the string "1" or "0" to disable 
  382. or enable all notes, respectively.
  383.  
  384. ExpandIncludes
  385. The default action is to expand Pascal include files in-line. This may not be desirable if 
  386. include files are being used to simulate modules. With ExpandIncludes =0, p2c attempts to 
  387. convert include files containing only whole procedures and global declarations into 
  388. analogous C include files. This may not always work, though; if you get error messages, 
  389. don't use this option. By combining this option with StaticFunctions =0, then doing some 
  390. fairly minor editing on the result, you can convert a pseudo-modular Pascal program into a 
  391. truly modular collection of C source files.
  392.  
  393. ElimDeadCode
  394. Some transformations that p2c does on the program may result in unreachable or "dead" 
  395. code. By default p2c removes such code, but sometimes it removes more than it should. If 
  396. you have "if false" segments which you wish to retain in C, you may have to set 
  397. ElimDeadCode =0.
  398.  
  399. .AnalyzeFlow
  400. By default p2c does some basic dataflow analysis on the program in an attempt to locate 
  401. code that can be simplified due to knowledge about the possible values of certain variables.  
  402. For example, a Pascal rewrite statement must translate to an if that either calls fopen on a 
  403. formerly closed file variable, or freopen on an already-open file.  If flow analysis can 
  404. prove that the file was open or closed upon entry to the statement, a much cleaner translation 
  405. is possible.
  406. It is possible that flow analysis will make simplifications that are undesirable or buggy.  If 
  407. this occurs, you can set AnalyzeFlow to 0 to disable this feature.
  408.  
  409. SkipIndices
  410. Normally Pascal arrays not based at zero are "shifted" down for C, preserving the total size 
  411. of the array. A Pascal array a[2..10] is translated to a C array a[9] with references like "a[i]" 
  412. changed to "a[i-2]" everywhere. If SkipIndices is set to a value of 2 or higher, this array 
  413. would instead be translated to a[11] with the first two elements never used. This 
  414. arrangement may generate incorrect code, though, for tricky source programs.
  415.  
  416. FoldConstants
  417. Pascal non-structured constants generally translate to #define 's in C. Set this to 1 to have 
  418. constants instantiated directly into the code. This may be turned on or off around specific 
  419. constant declarations. Set this to 0 to force p2c to make absolutely no assumptions about the 
  420. constant's value in generated code, so that you can change the constant later in the C code 
  421. without invalidating the translation. The default is to allow p2c to take advantage of its 
  422. knowledge of a constant's value, such as by generating code that assumes the constant is 
  423. positive.
  424.  
  425. CharConsts
  426. This governs whether single-character string literals in Pascal const declarations should be 
  427. interpreted as characters or strings. In other words, const a='x'; will translate to #define a 
  428. 'x' if CharConsts =1 (the default), or to #define a "x" if CharConsts =0. Note that if p2c 
  429. guesses wrong, the generated code will not be wrong, just uglier. For example, if a is 
  430. written as a character constant but it turns out to be used as a string, p2c will have to write 
  431. char-to-string conversion code each time the constant is used.
  432.  
  433. PreserveTypes
  434. P2c makes an attempt to retain the original names used for data types. For example,
  435.  
  436.      type foo = integer; bar = integer;
  437.  
  438. establishes two synonyms for the standard integer type; p2c does its best to preserve the 
  439. particular synonym that was used to declare each integer variable.  Because the Pascal 
  440. language treats these types as indistinguishable, there will be cases in the translation where 
  441. p2c must fall back on the "true" type, int. PreserveTypes and a few related options control 
  442. whether various kinds of type names are preserved.  The default settings preserve all type 
  443. names except for pointer types, which use "*" notation throught the program.  This reflects 
  444. the fact that Pascal forces pointer types to be named when traditionally they are not 
  445. separately named in C.
  446.  
  447. VarStrings
  448. In HP Pascal, a parameter of the form "var s : string" will match a string variable of any size; 
  449. a hidden size parameter is passed which may be accessed by the Pascal strmax function. You 
  450. can prevent p2c from creating a hidden size parameter by setting VarStrings =0. (Note that 
  451. each function uses the value of VarStrings as of the first declaration of the function that is 
  452. parsed, which is often in the interface section of a module.)
  453.  
  454. Prototypes
  455. Control whether ANSI C function prototypes are used. Default is according to AnsiC or 
  456. C++. This also controls whether to include parameter names or just their types in situations 
  457. where names are optional. The FullPrototyping parameter allows prototypes to be generated 
  458. for declarations but not for definitions (older versions of Lightspeed C required this). If you 
  459. use a mixture of prototypes and old-style definitions, types like short and float will be 
  460. promoted to int and double as required by the ANSI standard, unless PromoteArgs is used 
  461. to override this. The CastArgs parameter controls whether type-casts are used in function 
  462. arguments; by default they are used only if prototypes are not available.
  463.  
  464. StaticLinks
  465. HP Pascal and Turbo Pascal each include the concept of procedure or function pointers, 
  466. though with somewhat different syntaxes. P2c recognizes both notational styles. Another 
  467. difference is that HP's procedure pointers can point to nested procedures, while Turbo's can 
  468. point only to global procedures. In HP Pascal a procedure pointer must be stored as a struct 
  469. containing both a pure C function pointer and a "static link," a pointer to the parent 
  470. procedure's locals. (The static link is NULL for global procedures.) This notation can be 
  471. forced by setting StaticLinks =1. In Turbo, the default ( StaticLinks =0) is to use plain C 
  472. function pointers with no static links. A third option ( StaticLinks =2) uses structures with 
  473. static links, but assumes the links are always NULL when calling through a pointer (if you 
  474. need compatibility with the HP format but know your procedures are global).
  475.  
  476. SmallSetConst
  477. Pascal sets are translated into one of two formats, depending on the size of the set. If all 
  478. elements have ordinal values in the range 0..31, the set is translated as a single integer 
  479. variable using bit operations. (The SetBits parameter may be used to change the upper limit 
  480. of 31.) The SmallSetConst parameter controls whether these small-sets are used, and, if so, 
  481. how constant sets should be represented in C. For larger sets, an array of long is used. The 
  482. s [0] element contains the number of succeeding array elements which are in use. Set 
  483. elements in the range 0..31 are stored in the s [1] array element, and so on. Sets are 
  484. normalized so that s [ s [0]] is nonzero for any nonempty set. The standard run-time library 
  485. includes all the necessary procedures for operating on sets.
  486.  
  487. ReturnValueName
  488. This is one of many "naming conventions" parameters. Most of these take the form of a 
  489. printf -like string containing a %s where the relevant information should go. In the case of 
  490. ReturnValueName , the %s refers to a function name and the resulting string gives the name 
  491. of the variable to use to hold the function's return value. Such a variable will be made if a 
  492. function contains assignments to its return value buried within the body, so that return 
  493. statements cannot conveniently be used. Some parameters ( ReturnValueName included) do 
  494. not require the %s to be present in the format string; for example, the standard p2crc file 
  495. stores every function's return value in a variable called Result.
  496.  
  497. AlternateName
  498. P2c normally translates Pascal names into C names verbatim, but occasionally this is not 
  499. possible. A Pascal name may be a C reserved word or traditional C name like putc, or there 
  500. may be several like-named things that are hidden from each other by Pascal's scoping rules 
  501. but must be global in C. In these situations p2c uses the parameter AlternateName1 to 
  502. generate an alternative name for the symbol. The default is to add an underscore to the name. 
  503. There is also an AlternateName2 parameter for a second alternate name, and an 
  504. AlternateNamen parameter for the n th alternate name. (The value for this parameter should 
  505. include both a %s and a %d, in either order.) If these latter parameters are not defined, p2c 
  506. applies AlternateName1 many times over.
  507.  
  508. ExportSymbol
  509. Symbols in the interface section for a Pascal module are formatted according to the value of 
  510. ExportSymbol , if any. It is not uncommon to use modulename_%s for this symbol; the 
  511. default is %s, i.e., no special treatment for exported symbols. If you also define the 
  512. Export_Symbol parameter, that format is used instead for exported symbols which contain 
  513. an underscore character. If %S (with a capital "S") appears in the format string it stands for 
  514. the current module name.
  515.  
  516. Alias
  517. If the value of this parameter contains a %s, it is a format string applied to the names of 
  518. external functions or variables. If the value does not contain a %s, it becomes the name of 
  519. the next external symbol which is declared (after which the parameter is cleared).
  520.  
  521. Synonym
  522. This creates a synonym for another Pascal symbol or keyword. The format is
  523.  
  524.     Synonym old-name = new-name
  525.  
  526. All occurrences of old-name in the input text are treated as if they were new-name by the 
  527. parser. If new-name is a keyword, old-name will be an equivalent keyword. If new-name is 
  528. the name of a predefined function, old-name will behave in the same way as that function, 
  529. and so on. If new-name is omitted, then occurrences of old-name are entirely ignored in the 
  530. input file. Synonyms allow you to skip over a keyword in your dialect of Pascal that is not 
  531. understood by p2c, or to simulate a keyword or predefined identifier of your dialect with a 
  532. similar one that p2c recognizes. Note that all predefined functions are available at all times; if 
  533. you have a library routine that behaves like, e.g., Turbo Pascal's getmem procedure, you 
  534. can make your routine a synonym for getmem even if you are not translating in Turbo mode.
  535.  
  536. NameOf
  537. This defines the name to use in C for a specific symbol. It must appear before the symbol is 
  538. declared in the Pascal code; it is usually placed in the local p2crc file for the project. The 
  539. format is
  540.  
  541.     NameOf pascal-name = C-name
  542.  
  543. By default, Pascal names map directly onto C names with no change (except for the various 
  544. kinds of formatting outlined above). If the pascal-name is of the form module.name or 
  545. procedure.name then the command applies only to the instance of the Pascal name that is 
  546. global to that module, or local to that procedure. Otherwise, it applies to all usages of the 
  547. name.
  548.  
  549. VarMacro
  550. This is analogous to NameOf , but specifically for use with Pascal variables. The righthand 
  551. side can be most any C expression; all references to the variable are expanded into that C 
  552. expression. Names used in the C expression are taken verbatim. There is also a 
  553. ConstMacro parameter for translating constants as arbitrary expressions. Note that the 
  554. variable on the lefthand side must actually be declared in the program or in a module that it 
  555. uses. The declaration for the variable will be omitted from the generated code unless the 
  556. Pascal-name appears in the expression: If you ask to replace i with i+1, the variable i will 
  557. still be declared but its value will be shifted accordingly. Note that if i appears on the 
  558. lefthand side of an assignment, p2c will use algebra to "solve" for i.
  559.  
  560. In all cases where p2c parses C expressions, all C operators are recognized except 
  561. compound assignments like `+='. (Increment and decrement operators are allowed.) All 
  562. variable and function names are assumed to have integer type, even if they are names that 
  563. occur in the actual program. A type-specification operator `::' has been introduced; it has the 
  564. same precedence as `.' or `->' but the righthand side must be a Pascal type identifier (built-in 
  565. or defined by your program previously to when the macro definition was parsed), or an 
  566. arbitrary Pascal type expression in parentheses. The lefthand argument is then considered to 
  567. have the specified type. This may be necessary if your macro is used in situations where the 
  568. exact type of the expression must be known (say, as the argument to a writeln ).
  569.  
  570. FieldMacro
  571. Here the lefthand side must have the form record.field, where record is the Pascal type or 
  572. variable name for a record, and field is a field in that record. The righthand side must be a C 
  573. expression generally including the name record. All instances of that name are replaced by 
  574. the actual record being "dotted." For example,
  575.  
  576.     FieldMacro Rect.topLeft = topLeft(Rect)
  577.  
  578. translates a[i].topLeft into topLeft(a[i]), where a is an array of Rect.
  579.  
  580. FuncMacro
  581. The lefthand side must be any Pascal function or procedure name plus a parameter list. The 
  582. number of parameters must match the number in the function's uses and declaration. Calls to 
  583. the function are replaced by the C expression on the righthand side. For example,
  584.  
  585.     FuncMacro PtInRect(p,r) = PtInRect(p,&r)
  586.  
  587. causes the second argument of PtInRect to be passed by reference, even though the decla
  588. ration says it's not. If the function in question is actually defined in the program or module 
  589. being translated, the FuncMacro will not affect the definition but it will affect all calls to the 
  590. function elsewhere in the module. FuncMacros can also be applied to predefined or never-
  591. defined functions.
  592.  
  593. ReplaceBefore
  594. This option specifies a string replacement to be done on every Pascal
  595. source line.  For example:
  596.  
  597.     ReplaceBefore "{$ifdef" "{EMBED #ifdef"
  598.     ReplaceBefore "{$endif}" "{EMBED #endif}"
  599.  
  600. These lines rewrite Turbo Pascal compile-time conditionals into
  601. comments beginning with the special word EMBED.  This word instructs
  602. p2c to format the rest of the comment without "/* */" delimiters, i.e., the rest of the comment 
  603. is embedded directly in the output C program.  There is also a ReplaceAfter option, which 
  604. specifies replacements to be done on the output of p2c.
  605.  
  606. Currently, this feature makes only  literal string replacements, not pattern-based matches.  
  607. Some users of p2c have  found it useful  to feed their Pascal programs through  a more  
  608. powerful  editor  like sed or perl before giving them to p2c. Quite often this is all that is 
  609. necessary to get an acceptable translation  in the face of unrecognized Pascal dialects or 
  610. language features.
  611.  
  612.  
  613.  
  614. IncludeFrom
  615. This specifies that a given module's header should be included from a given place. The 
  616. second argument may be surrounded by " " or < > as necessary; if the second argument is 
  617. omitted, no include directive will be generated for the module.
  618.  
  619. ImportFrom
  620. This specifies that a given module's Pascal interface text can be found in the given file. The 
  621. named file should be either the source file for the module, or a specially prepared file with 
  622. the implementation section removed for speed. If no ImportFrom entry is found for a 
  623. module, the path defined by the ImportDir list is searched. Each entry in the path may 
  624. contain a %s, which expands to the name of the module. The default path looks for %s.pas 
  625. and %s.text in the current directory, then for --HOMEDIR--/%s.imp. (where --HOMEDIR-- 
  626. is the p2c home directory.)
  627.  
  628. StructFunction
  629. This parameter is a list of functions which follow the p2c semantics for structure-valued 
  630. functions (functions returning arrays, sets, and strings, and structs in primitive C dialects). 
  631. For these functions, a pointer to a return-value area is passed to the function as a special first 
  632. parameter. The function stores the result in this area, then returns a copy of the pointer. (The 
  633. standard C function strcpy is an example of this concept. Sprintf also behaves this way in 
  634. some dialects; it always appears on the StructFunction list regardless of the type of 
  635. implementation.) The system configuration file includes a list of common structured 
  636. functions so that p2c 's optimizer will know how to manipulate them.
  637.  
  638. StrlapFunction
  639. Functions on this list are structured functions as above, but with the ability to work in-place; 
  640. that is, the same pointer may be passed as both the return value area and a regular parameter.
  641.  
  642. Deterministic Functions on this list have no side effects or side dependencies. An example is 
  643. the sin function in the standard math library; two calls with the same parameter values 
  644. produce the same result, and have no effects other than returning a value. P2c can make use 
  645. of this knowledge when optimizing code for efficiency or readability. Functions on this list 
  646. are also assumed to be relatively fast, so that it is acceptable to duplicate a call to the 
  647. function.
  648.  
  649. LeaveAlone Functions on this list are not subjected to the normal built-in translation rules 
  650. that p2c would otherwise use. For example, adding writeln to this list would translate 
  651. writeln statements blindly into calls to a C writeln() function, rather than being translated into 
  652. equivalent printf calls. The built-in translation is also suppressed if the function has a 
  653. FuncMacro .
  654.  
  655. BufferedFile
  656. P2c normally assumes binary files will use read/write, not get/put/^ notation. A file buffer 
  657. variable will only be created for a file if buffer notation is used for it. For global file 
  658. variables this may be detected too late (a declaration without buffers may already have been 
  659. written). Such files can be listed in BufferedFile to force p2c to allocate buffers for them; do 
  660. this if you get a warning message that says it is necessary. Set BufferedFile =1 to buffer all 
  661. files, in which case UnBufferedFile allows you to force certain files not to have buffers.
  662.  
  663. StructFiles
  664. If p2c still can't translate your file operations correctly, you can set StructFiles =1 to cause 
  665. Pascal files to translate into structs which include the usual C FILE pointer, as well as file 
  666. buffer and file name fields. While the resulting code doesn't look as much like native C, the 
  667. file structs will allow p2c to do a correct translation in many more cases.
  668.  
  669. CheckFileEOF
  670. Normally only file-open operations are checked for errors. Additional error checking, such 
  671. as read-past-end-of-file, can be enabled with parameters like CheckFileEOF . These checks 
  672. can make the code very ugly! If I/O checking is enabled by the program ( "$iocheck on$" in 
  673. HP Pascal; {$I+} in Turbo; this is always the default state), these checks will generate fatal 
  674. errors unless enclosed in an HP Pascal try - recover construct. If I/O checking is disabled, 
  675. these will cause the global variable P_ioresult to be set zero or nonzero according to the 
  676. outcome. The default for most of these options is to check only when I/O checking is 
  677. enabled.
  678.  
  679.  
  680.  
  681. ISSUES
  682.  
  683. Integer size.
  684. P2c normally generates code to work with either 16 or 32 bit ints. If you know your C integers 
  685. will be 16 or 32 bits, set IntSize appropriately. In particular setting IntSize =32 will generate 
  686. much cleaner code: p2c no longer must carefully cast function arguments between int and long. 
  687. These casts also will be unnecessary if ANSI prototypes are available. To disable int/long 
  688. casting because you know at least one of these cases will hold, set CastLongArgs =0. (The 
  689. CastArgs parameter similarly controls other types of casts, such as between ints and doubles.) 
  690. The Integer16 parameter controls whether Pascal integers are interpreted as 16 or 32 bits, or 
  691. translated as native C integers. The default value depends on the Language selected.
  692.  
  693. Signed/unsigned chars.
  694. Pascal characters are normally "weakly" interpreted as unsigned; this is controlled by 
  695. UnsignedChar . The default is "either," so that C's native char type may be used even if its 
  696. signed-ness is unknown. Code that uses characters outside of the range 0-127 may need a 
  697. different setting. Alternatively, you can use the types {SIGNED} char and {UNSIGNED} char 
  698. in the few cases where it really matters. These comments are controlled by the SignedComment 
  699. and UnsignedComment parameters. (The type {UNSIGNED} integer is also recognized.) The 
  700. SignedChar parameter tells whether C characters are signed or unsigned (default is 
  701. "unknown"). The HasSignedChar parameter tells whether the phrase "signed char" is legal in 
  702. the output. If it is not, p2c may have to translate Pascal signed bytes into C shorts.
  703.  
  704. Special types.
  705. P2c understands the following predefined Pascal type names: 
  706.     integer    signed integers depending on Integer16 ; 
  707.     longint    signed 32-bit integers; 
  708.     unsigned    unsigned 32-bit integers; 
  709.     sword    signed 16-bit integers; 
  710.     word    unsigned 16-bit integers; 
  711.     c_int    signed native C integers; 
  712.     c_uint    unsigned native C integers; 
  713.     sbyte    signed 8-bit integers; 
  714.     byte    unsigned 8-bit integers; 
  715.     real    floating-point numbers depending on DoubleReals ;     
  716.     single    single-precision floats; 
  717.     longreal , 
  718.     double , 
  719. and    extended    double-precision floats;     
  720.     pointer 
  721. and    anyptr    generic pointers (assignment-compatible with any pointer type); 
  722.     string    generic string of length StringDefault (normally 255); 
  723. also, the usual Pascal types 
  724.     char
  725.     boolean,
  726. and 
  727.     text . 
  728. (If your Pascal uses different names for these concepts, the Synonym option will come in 
  729. handy.)
  730.  
  731. Embedded code. 
  732. It is possible to write a Pascal comment containing C code to be embedded into the output. See 
  733. the descriptions of EmbedComment and its relatives in the system p2crc file. These techniques 
  734. are helpful if you plan to do repeated translations of code that is still being maintained in Pascal.
  735. See the description of ReplaceBefore for an example use of embedded code.
  736.  
  737. Comments and blank lines. 
  738. P2c collects the comments in a procedure into a list. All comments and statements are stamped 
  739. with serial numbers which are used to reattach comments to statements even after code has been 
  740. added, removed, or rearranged during translation. "Orphan" comments attached to statements 
  741. that have been lost are attached to nearby statements or emitted at the end of the procedure. 
  742. Blank lines are treated as a kind of comment, so p2c will also reproduce your usage of blank 
  743. lines. If the comment mechanism goes awry, you can disable comments with EatComments or 
  744. disable their being attached to code with SpitComments .
  745.  
  746. Indentation. 
  747. P2c has a number of parameters to govern indentation of code. The default values produce the 
  748. GNU Emacs standard indentation style, although p2c can do a better job since it knows more 
  749. about the code it is indenting. Indentation works by applying "indentation deltas," which are 
  750. either absolute numbers (which override the previous indentation), or signed relative numbers 
  751. (which augment the previous indentation). A delta of "+0" specifies no change in indentation. 
  752. All of the indentation options are described in the standard p2crc file.
  753.  
  754. Line breaking. 
  755. P2c uses an algorithm similar to the TeX typesetter's paragraph formatter for breaking long 
  756. statements into multiple lines. A "penalty" is assigned to various undesirable aspects of all 
  757. possible line breaks; the "badness" of a set of line breaks is approximately the sum of all the 
  758. penalties. Chief among these are serious penalties for overrunning the desired maximum line 
  759. length (default 78 columns), an infinite penalty for overrunning the absolute maximum line 
  760. length (default 90), and progressively greater penalties for breaking at operators deeply nested in 
  761. expressions. Parameters such as OpBreakPenalty control the relative weights of various 
  762. choices. BreakArith and its neighbors control whether the operator at a line break should be 
  763. placed at the end of the previous line or at the beginning of the next. If you don't want any 
  764. oversize lines, define MaxLineWidth =78.
  765.  
  766. Unlike TeX, p2c 's line breaker must actually try all possible sets of break points. To avoid 
  767. excessive computation, the total penalty contributed at each decision point must sum to a 
  768. nonnegative value; negative values are clipped up to zero. This allows p2c to prune away 
  769. obviously undesirable alternatives in advance. The MaxLineBreakTries parameter (default 
  770. 5000) controls how many alternatives to try before giving up and using the best so far.
  771.  
  772. PASCAL_MAIN. 
  773. P2c generates a call to this function at the front of the main program. In the (unmodified) run-
  774. time library all this does is save argc and argv away because in both HP and Turbo these are 
  775. accessed as global variables. If you do not wish to use this feature, define ArgCName to be 
  776. argc, ArgVName to be argv, and MainName (normally "PASCAL_MAIN") to be blank. This 
  777. will work if argc and argv are never accessed outside of your main program. BUGS P2c was 
  778. designed with the idea that clean, readable output in most cases is worth more than guaranteed 
  779. correct output in extreme cases. P2c is not a compiler! However, ideally the "extreme" cases 
  780. would include only those which never arise in real life. Thus if p2c actually generates incorrect 
  781. code I will consider it a bug, but I will not apologize for it. :-) Below are the major remaining 
  782. cases where this is known to occur.
  783.  
  784. Certain kinds of conformant array parameters (including multi-dimensional conformant arrays) 
  785. produce code that declares variable-length arrays in C. Only a few C compilers, such as the 
  786. GNU C compiler, support this language extension. Otherwise some hand re-coding will be 
  787. required.
  788.  
  789. HP Pascal try - recover structures are translated into calls to TRY and RECOVER macros, 
  790. which are defined to simulate the construct using setjmp and longjmp. If this emulation does not 
  791. work, define the symbol FAKE_TRY to cause these macros to become "inert." (In cases where 
  792. the error is detected by code physically within the body of the try statement, a C goto to the 
  793. recover section is always generated.) Also, local file variables in scopes which are destroyed by 
  794. an escape are not closed.
  795.  
  796. Non-local GOTO's and try - recover statements are each implemented, but may conflict if both 
  797. are used at once. Non-local GOTO's are fairly careful about closing files that go out of scope but 
  798. may fail to do so in the presence of recursion.
  799.  
  800. Arrays containing files are not initialized to NULL as other files are. In some cases, such as file 
  801. variables allocated by NEW, the file is initialized but not automatically closed by DISPOSE.
  802.  
  803. LINK variables allowing sub-procedures access to their parents' variables are occasionally 
  804. omitted by mistake, if the access is too indirect for p2c to notice. If this happens, you can add an 
  805. explicit reference to a parent variable in the sub-procedure. A statement of the form "a:=a" will 
  806. count as a reference but then be optimized away by p2c.
  807.  
  808. Many aspects of Modula-2 are translated only superficially. For example, the type-compatibility 
  809. properties of the WORD and ARRAY OF WORD types are only roughly modelled, as are the 
  810. scope rules concerning modules.
  811.  
  812. Parts of VAX Pascal are still untreated. In particular, the [UNSAFE] attribute and a few others 
  813. are not fully supported, nor are the semantics of the OPEN procedure.
  814.  
  815. Turbo and VAX Pascal's double, quadruple, and extended real types all translate to the C double 
  816. type. Turbo's computational type is not supported at all.
  817.  
  818. Because Pascal strings (with length bytes) are translated into C strings (with null terminators), 
  819. certain Pascal string tricks will not work in the translated code. For example the assignment 
  820. s[0]:=chr(x) is translated to s[x]=0 on the assumption that the string is being shortened. If x is 
  821. actually greater than the current length, but not of a recognizable form like ord(s[0])+n, then the 
  822. generated code will not work. In VAX Pascal this corresponds to performing arithmetic on the 
  823. LENGTH field of a varying-length string.
  824.  
  825. Turbo Pascal's automatic clipping of strings is not supported. In Turbo, if a ten character string 
  826. is assigned to a string[8] variable, the last two characters are silently removed. The code 
  827. produced by p2c generally will overrun the target string instead! The StringTruncLimit 
  828. parameter (80 by default if Language = Turbo ) specifies a string size which should be 
  829. considered "short"; assignments of potentially-long strings to short string variables will cause a 
  830. warning but will not automatically truncate. The cure is to use copy in the Pascal source to 
  831. truncate the strings explicitly. 
  832.  
  833. FILES
  834.  
  835. file.xxx    Pascal source files
  836. file.c    resulting C source file 
  837. module.h    resulting C header file
  838. p2c Preferences    local configuration file 
  839. p2c Preferences    in the System preferences folder: system-wide configuration file 
  840. system.imp    declarations for predefined functions
  841. system.m2    analogous declarations for Modula-2
  842. *.imp    interface text for standard modules; (mpw.imp for MPW)
  843. p2c.h    header file for translated programs
  844. libp2c.a    run-time library
  845.  
  846. AUTHOR 
  847.     Dave Gillespie, daveg@synaptics.com.
  848.     Günther Sawitzki, gs@statlab.uni-heidelberg.de
  849.  
  850. Many thanks to William Bader, Steven Levi, Rick Koshi, Eric Raymond, Magne Haveraaen, 
  851. Dirk Grunwald, David Barto, Paul Fisher, Tom Schneider, Dick Heijne, and many others 
  852. whose suggestions and bug reports have helped improve p2c in countless ways.
  853.  
  854. The original Unix version of p2c can be found on csvax.cs.caltech.edu.
  855.  
  856.